package com.ruoyi.match.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.match.domain.vo.InviteVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.match.domain.Invite;
import com.ruoyi.match.service.IInviteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邀请Controller
 * 
 * @author zhangrui
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/match/invite")
public class InviteController extends BaseController
{
    @Autowired
    private IInviteService inviteService;

    /**
     * 查询邀请列表
     */
//    @PreAuthorize("@ss.hasPermi('match:invite:list')")
    @GetMapping("/list")
    public TableDataInfo list(Invite invite)
    {
        startPage();
        List<Invite> list = inviteService.selectInviteList(invite);
        return getDataTable(list);
    }

    /**
     * 导出邀请列表
     */
    @PreAuthorize("@ss.hasPermi('match:invite:export')")
    @Log(title = "邀请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Invite invite)
    {
        List<Invite> list = inviteService.selectInviteList(invite);
        ExcelUtil<Invite> util = new ExcelUtil<Invite>(Invite.class);
        util.exportExcel(response, list, "邀请数据");
    }

    /**
     * 获取邀请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('match:invite:query')")
    @GetMapping(value = "/{fromId}")
    public AjaxResult getInfo(@PathVariable("fromId") Long fromId)
    {
        return success(inviteService.selectInviteByFromId(fromId));
    }

    /**
     * 新增邀请
     */
//    @PreAuthorize("@ss.hasPermi('match:invite:add')")
    @Log(title = "邀请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Invite invite)
    {
        System.out.println("invite:"+invite);
        invite.setFromId(SecurityUtils.getUserId());
        return toAjax(inviteService.insertInvite(invite));
    }

    /**
     * 修改邀请
     */
//    @PreAuthorize("@ss.hasPermi('match:invite:edit')")
    @Log(title = "邀请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Invite invite)
    {
        return toAjax(inviteService.updateInvite(invite));
    }

    /**
     * 删除邀请
     */
//    @PreAuthorize("@ss.hasPermi('match:invite:remove')")
    @Log(title = "邀请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fromIds}")
    public AjaxResult remove(@PathVariable Long[] fromIds)
    {
        return toAjax(inviteService.deleteInviteByFromIds(fromIds));
    }

    /**
     * 查询我发出的邀请列表
     */
    @GetMapping("/myInvite/list")
    public TableDataInfo showMyInviteList()
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<InviteVo> list = inviteService.showMyInviteList(userId);
        System.out.println("list:"+list);
        return getDataTable(list);
    }


    /**
     * 查询我收到的邀请列表
     */
    @GetMapping("/InviteMe/list")
    public TableDataInfo showInviteMeList()
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<InviteVo> list = inviteService.showInviteMeList(userId);
        return getDataTable(list);
    }


    /**
     * 接受邀请
     * */
    @PostMapping("/InviteMe/accept")
    public AjaxResult acceptInviting(@RequestBody Invite invite)
    {
        return toAjax(inviteService.acceptInviting(invite));
    }


    /**
     * 拒绝邀请
     * */
    @PostMapping("/InviteMe/reject")
    public AjaxResult rejectInviting(@RequestBody Invite invite)
    {
        return toAjax(inviteService.rejectInviting(invite));
    }
}
